using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DatabaseMemoryChecker
{
    public partial class MainForm : Form
    {
        int j = 0;
        int _AvailableVirtual_t0=0;
        int _AVBeforeTableFill = 0;
        int _DeltaAV = 0;
        int _oldRow = 0;
        int _RowSelected = -1;

        DataGridCell dgCell = new DataGridCell();
        DataView dv;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int q = 1000;
            string x = q.ToString();
            _AVBeforeTableFill = NativeMethods.AvailVirtual();
            this.addressBookTableAdapter1.Fill(this.dataSet1.AddressBook);
            //timer1.Enabled = true;
            _AvailableVirtual_t0 = NativeMethods.AvailVirtual();
            //labelAfterFill.Text = _AvailableVirtual_t0.ToString();
            Console.WriteLine(x);
            //listBox1.SelectedIndex = 0;
            DataGridSetup();
            listBox1.Hide();
        }

        private void AddDataColumn(DataTable table)
        {
            System.Type decimalType;
            decimalType = System.Type.GetType("System.Decimal");

            // Create the column. The name is 'Tax,' with data type Decimal,and 
            // an expression ('UnitPrice * .0862) to calculate the tax.
            DataColumn column = new DataColumn("Tax",
                decimalType, "UnitPrice * .0862");

            // Set various properties.
            column.AutoIncrement = false;
            column.ReadOnly = true;

            // Add to Columns collection.;
            this.dataSet1.AddressBook.Columns.Add(column);
        }

        private void AddDataColumn2()
        {
            System.Type stringType;
            stringType = System.Type.GetType("System.String");

            // Create the column. The name is 'Tax,' with data type Decimal,and 
            // an expression ('UnitPrice * .0862) to calculate the tax.
            DataColumn column = new DataColumn("NameEmail",
                stringType, "PersonName + EmailAddress");

            // Set various properties.
            column.AutoIncrement = false;
            column.ReadOnly = true;

            // Add to Columns collection.;
            this.dataSet1.AddressBook.Columns.Add(column);
            listBox1.DisplayMember = "NameEmail";
        }

        public void DataGridSetup()
        {
            dv = new DataView(dataSet1.Tables["AddressBook"]);
            label2.Text = this.dataSet1.AddressBook.Rows.ToString();
            dataGridTwoColumns.Focus();
            HideDataGridVerticalScrollBar(dataGridTwoColumns);
            if (GetCountOfRowsInDataGrid(bindingSource1.Filter) > 0)
            {
                dgCell.RowNumber = 0;
                dataGridTwoColumns.Select(dgCell.RowNumber);
            }
            else
                _RowSelected = -1;
        }

        public void HideDataGridVerticalScrollBar(DataGrid dg)
        {
            foreach (Control g in dg.Controls)
            {
                if (g.GetType() == typeof(System.Windows.Forms.VScrollBar))
                    g.Width = 0;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
  
            for (int i = 1; i <= 10; i++)
            {
                try
                {
                    this.addressBookTableAdapter1.Insert(j++, i, "P", i.ToString(), "qPresso@abc.com");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.StackTrace);
                }
            }
            this.addressBookTableAdapter1.Fill(this.dataSet1.AddressBook);
            label2.Text = this.dataSet1.AddressBook.Rows.Count.ToString();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            // Update Time

            // Update Records Adde
            //textBoxRecordsAdded.Text = j.ToString();
            //textBoxRecordsAdded.Update();

            // Update Memory add
            _DeltaAV = _AvailableVirtual_t0 - NativeMethods.AvailVirtual();
            textBoxMemory.Text = _DeltaAV.ToString();
        }

        private void btnTruncateTable_Click(object sender, EventArgs e)
        {
            this.addressBookTableAdapter1.DeleteAllQuery();
            this.addressBookTableAdapter1.Fill(this.dataSet1.AddressBook);
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            this.bindingSource1.Filter = String.Empty;
            //this.bindingSource1.Filter = "DriverID="+textBoxFilter.Text;
            //this.addressBookTableAdapter1.Fill(this.dataSet1.AddressBook);
        }

        private void dataGrid1_Paint(object sender, PaintEventArgs e)
        {
           // e.ClipRectangle.
        }

        private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
        {
            DataGrid.HitTestInfo hti = dataGridTwoColumns.HitTest(e.X, e.Y);

            if (hti.Type == DataGrid.HitTestType.Cell)
            {
                dataGridTwoColumns.CurrentCell = new DataGridCell(hti.Row, hti.Column);
                dataGridTwoColumns.Select(hti.Row);
                label4.Text = dataGridTwoColumns[hti.Row, hti.Column].ToString();
                labelRow.Text = label4.Text;
            }
        }
        
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            bindingSource1.Filter = "PersonName like '" + textBoxSearch.Text + "%'";
            int rowCount = GetCountOfRowsInDataGrid(bindingSource1.Filter);

            if (rowCount > 0)
            {
                dgCell.RowNumber = 0;
                dataGridTwoColumns.CurrentCell = dgCell;
                dgCell.RowNumber = 0;
                SelectRow(dgCell.RowNumber);
            }
            else
                _RowSelected = -1;
        }

        private int GetCountOfRowsInDataGrid(string filter)
        {
            
            DataView dv = new DataView(dataSet1.Tables["AddressBook"]);
            dv.RowFilter = filter;

            return dv.Count;
        }

        private void dataGridTwoColumns_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case  Keys.Home:
                    DoHome();
                    e.Handled = true;
                    break;
                case  Keys.Up:
                case  Keys.Right:
                case  Keys.Left:
                case  Keys.Down:
         
                    if(GetCountOfRowsInDataGrid(bindingSource1.Filter)>0)
                        SelectRow(dataGridTwoColumns.CurrentCell.RowNumber);
                    e.Handled = true;
                    break;
            
                case Keys.End:
                    DoEnd();
                    e.Handled = true;
                    break;
            }
            base.OnKeyDown(e);

        }

        private void panelHome_Click(object sender, EventArgs e)
        {
            DoHome();
        }

        private void panelUp_Click(object sender, EventArgs e)
        {
            DoUp();
        }

        private void panelDown_Click(object sender, EventArgs e)
        {
            DoDown();
        }

        private void panelEnd_Click(object sender, EventArgs e)
        {
            DoEnd();
        }

        private void DoHome()
        {
            dataGridTwoColumns.Focus();
            int rowCount = GetCountOfRowsInDataGrid(bindingSource1.Filter);

            if (rowCount > 0)
            {
                dgCell.RowNumber = 0;
                dataGridTwoColumns.CurrentCell = dgCell;
                dgCell.RowNumber = 0;
                SelectRow(dgCell.RowNumber);
            }
            else
                _RowSelected = -1;
        }

        private void DoUp()
        {
            int rowCount = GetCountOfRowsInDataGrid(bindingSource1.Filter);

            dgCell = dataGridTwoColumns.CurrentCell;
            if (dgCell.RowNumber > 0)
            {
                dgCell.RowNumber -= 1;
                dataGridTwoColumns.CurrentCell = dgCell;
                SelectRow(dgCell.RowNumber);
            }     
        }

        private void DoDown()
        {
            dgCell = dataGridTwoColumns.CurrentCell;
            int rowCount = GetCountOfRowsInDataGrid(bindingSource1.Filter);

            // Move down a row if you can
            if (dgCell.RowNumber < (rowCount-1))
            {
                dgCell.RowNumber += 1;
                dataGridTwoColumns.CurrentCell = dgCell;
                SelectRow(dgCell.RowNumber);
            }
        }

        private void DoEnd()
        {
            dataGridTwoColumns.Focus();
            int rowCount = GetCountOfRowsInDataGrid(bindingSource1.Filter);
            if (rowCount > 0)
            {
                dgCell.RowNumber = rowCount - 1;
                SelectRow(dgCell.RowNumber);
            }
        }

        private void SelectRow(int row)
        {
            dataGridTwoColumns.CurrentRowIndex = row;
            dataGridTwoColumns.UnSelect(_oldRow);
            dataGridTwoColumns.Select(row);
            _oldRow = row;
            labelRow.Text = row.ToString();
        }

        private void panelHome_GotFocus(object sender, EventArgs e)
        {

        }

        private void btnAddColumn_Click(object sender, EventArgs e)
        {
            AddDataColumn2();
        }
    }
}